<?php

namespace App\Utils;

use App\Models\Admin;
use App\Models\MSlugClassify;

/**
 * 系统菜单配置
 */
class MenuUtil
{
    /**
     * 后台系统菜单配置
     */
    public static function adminMenu()
    {
        $classify = MSlugClassify::all();
        $slugMenu = [];
        foreach ($classify as $c) {
            array_push($slugMenu, ["title" => $c->title, "url" => route('admin.slug', ['cid' => $c->id]), "permission" => "permission.slug"]);
        }
        $menu = array(
//            ["title" => "工作台",
//                "icon" => "admin/imgs/icon01.png",
//                "url" => route('admin.user.count'),
//                "permission" => "purview.worker",
//                "menus" => [
//                    ["title" => "系统统计",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "会员统计",
//                                "url" => route('admin.user.count'),
//                                "permission" => "permission.users"],
//                            ["title" => "资金报表",
//                                "url" => route('admin.money.statement'),
//                                "permission" => "permission.users"],
//                        ]
//                    ],
//                    ["title" => "提现处理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "待处理提现",
//                                "url" => route('admin.withdraw', ['flag' => 0]),
//                                "permission" => "permission.withdraw"],
//                            ["title" => "已打款提现记录",
//                                "url" => route('admin.withdraw', ['flag' => 1]),
//                                "permission" => "permission.withdraw"],
//                            ["title" => "已拒绝提现记录",
//                                "url" => route('admin.withdraw', ['flag' => 2]),
//                                "permission" => "permission.withdraw"],
//                        ]
//                    ],
//                    ["title" => "账单管理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "充值/扣款",
//                                "url" => route('admin.debit.page'),
//                                "permission" => "permission.debit"],
//                            ["title" => "充值/扣款记录",
//                                "url" => route('admin.debit.log'),
//                                "permission" => "permission.debit"],
//                            ["title" => "奖金明细",
//                                "url" => route('admin.bonus'),
//                                "permission" => "permission.bonus"],
//                            ["title" => "消费明细",
//                                "url" => route('admin.consume'),
//                                "permission" => "permission.consume"],
//                            ["title" => "转账明细",
//                                "url" => route('admin.transfer'),
//                                "permission" => "permission.transfer"],
//                        ]
//                    ],
//                    ["title" => "打款申请管理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "未处理申请单",
//                                "url" => route('admin.remit', ['flag' => 0]),
//                                "permission" => "permission.remit"],
//                            ["title" => "已处理申请单",
//                                "url" => route('admin.remit', ['flag' => 1]),
//                                "permission" => "permission.remit"],
//                        ]
//                    ],
//                    ["title" => "商品管理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "添加新商品",
//                                "url" => route('admin.product'),
//                                "permission" => "permission.users"],
//                            ["title" => "在售商品列表",
//                                "url" => route('admin.product.list', ['flag' => 0]),
//                                "permission" => "permission.users"],
//                            ["title" => "下架商品列表",
//                                "url" => route('admin.product.list', ['flag' => 1]),
//                                "permission" => "permission.users"],
//                            ["title" => "未发货订单",
//                                "url" => route('admin.order', ['flag' => 0]),
//                                "permission" => "permission.order"],
//                            ["title" => "已发货订单",
//                                "url" => route('admin.order', ['flag' => 1]),
//                                "permission" => "permission.order"],
//                        ]
//                    ],
//                ]
//            ],
            //=====================================================
//            ["title" => "会员管理",
//                "icon" => "admin/imgs/icon02.png",
//                "url" => route('admin.users', ['flag' => 1]),
//                "permission" => "purview.user",
//                "menus" => [
//                    ["title" => "基本会员管理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "全部会员列表",
//                                "url" => route('admin.users', ['flag' => 1]),
//                                "permission" => "permission.users"],
//                            ["title" => "未激活会员",
//                                "url" => route('admin.users', ['flag' => 2]),
//                                "permission" => "permission.users"],
//                            ["title" => "已冻结会员",
//                                "url" => route('admin.users', ['flag' => 3]),
//                                "permission" => "permission.users"],
//                            ["title" => "已禁用会员",
//                                "url" => route('admin.users', ['flag' => 4]),
//                                "permission" => "permission.users"],
//                            ["title" => "内部会员列表",
//                                "url" => route('admin.users', ['flag' => 5]),
//                                "permission" => "permission.users"]
//                        ]
//                    ],
//                    ["title" => "会员操作",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
////                      ["title"      => "注册内部会员",
////                       "url"        => route('admin.reg'),
////                       "permission" => "permission.reg"],
////                            ["title" => "会员信息采集",
////                                "url" => route('admin.info'),
////                                "permission" => "permission.info"],
//                            ["title" => "升级指定会员",
//                                "url" => route('admin.vipup'),
//                                "permission" => "permission.vipup"],
//                            ["title" => "会员系谱图",
//                                "url" => route('admin.node.tree'),
//                                "permission" => "permission.nodetree"],
//                            ["title" => "会员树状图",
//                                "url" => route('admin.node.zTree'),
//                                "permission" => "permission.ztree"],
//                        ]
//                    ],
//                    ["title" => "报单中心管理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "申请中的处理",
//                                "url" => route('admin.apply.bdzx', ['flag' => 0]),
//                                "permission" => "permission.bdzx"],
//                            ["title" => "通过申请的报单中心",
//                                "url" => route('admin.apply.bdzx', ['flag' => 1]),
//                                "permission" => "permission.bdzx"],
//                            ["title" => "已拒绝的报单中心",
//                                "url" => route('admin.apply.bdzx', ['flag' => 2]),
//                                "permission" => "permission.bdzx"],
//                        ]
//                    ],
//                ]],
            //=====================================================
            ["title" => "信息管理",
                "icon" => "admin/imgs/icon04.png",
                "url" => route('admin.classify'),
                "permission" => "purview.news",
                "menus" => [
                    ["title" => "基础信息设置",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => [
                            ["title" => "信息分类",
                                "url" => route('admin.classify'),
                                "permission" => "permission.classify"],
                            ["title" => "文章列表",
                                "url" => route('admin.news'),
                                "permission" => "permission.news"],
                            ["title" => "系统公告",
                                "url" => route('admin.notices'),
                                "permission" => "permission.bulletin"],
                        ]
                    ],
//                    ["title" => "内容碎片管理",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "碎片分类",
//                                "url" => route('admin.slug.classify'),
//                                "permission" => "permission.slug.classify"],
//                            ["title" => "碎片列表",
//                                "url" => route('admin.slug'),
//                                "permission" => "permission.slug"],
//                            ["title" => "系统公告",
//                                "url" => route('admin.notices'),
//                                "permission" => "permission.bulletin"],
//                        ]
//                    ],
                    ["title" => "会员反馈",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => [
                            ["title" => "咨询列表",
                                "url" => route('admin.feedback', ['flag' => 0]),
                                "permission" => "permission.feedback"],
                            ["title" => "回复列表",
                                "url" => route('admin.feedback', ['flag' => 1]),
                                "permission" => "permission.feedback"],
                        ]
                    ],
//                    ["title" => "系统相关协议",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "会员注册协议",
//                                "url" => route('admin.protocol'),
//                                "permission" => "permission.feedback"]
//                        ]
//                    ]
                ]],
            //=====================================================
            ["title" => "碎片管理",
                "icon" => "admin/imgs/icon04.png",
                "url" => route('admin.slug.classify'),
                "permission" => "purview.slugs",
                "menus" => [
                    ["title" => "碎片分类管理",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => [
                            ["title" => "碎片分类",
                                "url" => route('admin.slug.classify'),
                                "permission" => "permission.slug.classify"],

                        ]
                    ],
                    ["title" => "碎片内容管理",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => $slugMenu
                    ]
                ]],
            //=====================================================
            ["title" => "模式管理",
                "icon" => "admin/imgs/icon03.png",
                "url" => route('admin.email'),
                "permission" => "purview.rule",
                "menus" => [
                    ["title" => "参数设置",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => [
//                            ["title" => "会员等级设定",
//                             "url" => route('admin.vips'),
//                             "permission" => "permission.mode"],
//                            ["title" => "模式参数设置",
//                                "url" => route('admin.mode'),
//                                "permission" => "permission.mode"],
//                            ["title" => "对公账户设置",
//                                "url" => route('admin.receipt'),
//                                "permission" => "permission.mode"],
//                            ["title"      => "会员升级设置",
//                                "url"        => route('admin.vip.config'),
//                                "permission" => "permission.mode"],
//                            ["title" => "系统功能开关",
//                                "url" => route('admin.switch'),
//                                "permission" => "permission.switch"],
                            ["title" => "系统邮箱配置",
                                "url" => route('admin.email'),
                                "permission" => "permission.email"],

                        ]
                    ]
                ]],
            //=====================================================
            ["title" => "系统设置",
                "icon" => "admin/imgs/icon06.png",
                "url" => route('admin.setting'),
                "permission" => "purview.setting",
                "menus" => [
                    ["title" => "站点管理",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => [
                            ["title" => "站点信息设置",
                                "url" => route('admin.setting'),
                                "permission" => "permission.webinfo"],
//                            ["title" => "登录界面皮肤",
//                                "url" => route('admin.login.theme'),
//                                "permission" => "permission.logintheme"],
                            ["title" => "一键初始化",
                                "url" => route('admin.initial'),
                                "permission" => "permission.initial"],
//                            ["title" => "一键测试系统",
//                                "url" => route('admin.system.testPage'),
//                                "permission" => "permission.test"]
                        ]
                    ],
                    ["title" => "安全管理",
                        "icon" => "admin/imgs/leftico01.png",
                        "menus" => [
                            ["title" => "管理员",
                                "url" => route('admin.admins'),
                                "permission" => "permission.admins"],
                            ["title" => "角色管理",
                                "url" => route('admin.roles'),
                                "permission" => "permission.roles"],
                            ["title" => "系统日志",
                                "url" => route('admin.logs'),
                                "permission" => "permission.logs"]
                        ]
                    ],
//                    ["title" => "数据安全",
//                        "icon" => "admin/imgs/leftico01.png",
//                        "menus" => [
//                            ["title" => "数据导出",
//                                "url" => route('admin.export'),
//                                "permission" => "permission.export"],
//                            ["title" => "数据库备份",
//                                "url" => route('admin.sqls'),
//                                "permission" => "permission.database"]
//                        ]
//                    ]
                ]],
        );
        return $menu;
    }

    /**
     * 前台系统菜单配置
     */
    public static function systemMenu()
    {
        $menu = array(
            ["title" => "我的主页",
                "icon" => "icon-home",
                "url" => route('sys.center'),
                "menus" => null
            ],
            //==============================================
            ["title" => "财务中心",
                "icon" => "icon-caiwu",
                "url" => "#",
                "menus" => [
                    ["title" => "奖金记录",
                        "url" => route('sys.bonus')],
                    ["title" => "消费记录",
                        "url" => route('sys.shopping')],
                    ["title" => "会员转账",
                        "url" => route('sys.transfer')],
                    ["title" => "转账记录",
                        "url" => route('sys.transfer.log')],
                    ["title" => "申请提现",
                        "url" => route('sys.withdraw')],
                    ["title" => "提现记录",
                        "url" => route('sys.withdraw.log')],
                    ["title" => "币种转换",
                        "url" => route('sys.switch.coin')],
                    ["title" => "转换记录",
                        "url" => route('sys.switch.log')],
                    ["title" => "打款申请",
                        "url" => route('sys.remit.apply')],
                    ["title" => "打款申请记录",
                        "url" => route('sys.remit.logs')],
                ]],
            //==============================================
            ["title" => "会员管理",
                "icon" => "icon-user",
                "url" => "#",
                "menus" => [
                    ["title" => "注册新会员",
                        "url" => route('sys.reg.page'),
//                  "new"   => true
                    ],
                    ["title" => "会员报单",
                        "url" => route('sys.users', ['flag' => 1])],
                    ["title" => "已报单会员",
                        "url" => route('sys.users', ['flag' => 2])],
                    ["title" => "我的推荐会员",
                        "url" => route('sys.users', ['flag' => 3])],
                    ["title" => "申请报单中心",
                        "url" => route('sys.bdzx')],
                    ["title" => "会员节点关系图",
                        "url" => route('sys.node.tree', ['uid' => ss('uid')])],
                    ["title" => "会员推荐关系图",
                        "url" => route('sys.node.zTree')],
                ]],
            //==============================================
            ["title" => "虚拟货币",
                "icon" => "icon-user",
                "url" => "#",
                "menus" => [
                    ["title" => "交易大厅",
                        "url" => route('sys.stock')],
//                 ["title" => "我的持仓",
//                  "url"   => route('sys.stock')],
                ]],
            //==============================================
            ["title" => "购物商城",
                "icon" => "icon-shangcheng",
                "url" => "#",
                "menus" => [
                    ["title" => "购物中心",
                        "url" => route('sys.products')],
                    ["title" => "我的购物车",
                        "url" => route('sys.carts')],
                    ["title" => "我的订单",
                        "url" => route('sys.orders')],
                    ["title" => "我的收货地址",
                        "url" => route('sys.address')]
                ]],
            //==============================================
            ["title" => "个人信息",
                "icon" => "icon-kerenxinxiguanli",
                "url" => "#",
                "menus" => [
                    ["title" => "我的信息修改",
                        "url" => route('sys.information')],
                    ["title" => "修改密码",
                        "url" => route('sys.pass')]
                ]],
            //==============================================
            ["title" => "关于我们",
                "icon" => "icon-trophy",
                "url" => "#",
                "menus" => [
                    ["title" => "新闻动态",
                        "url" => route('sys.news', ['flag' => 0])],
                    ["title" => "系统公告",
                        "url" => route('sys.news', ['flag' => 1])],
                    ["title" => "在线留言",
                        "url" => route('sys.feedback')]
                ]],
            //==============================================
            ["title" => "安全退出",
                "icon" => "icon-tuichu",
                "url" => route('sys.login.exit')
            ],
        );
        return $menu;
    }


    public static function webMenu()
    {
        $menu = array(
            ["title" => "我的主页",
                "icon" => "icon-icon1",
                "url" => route('web.center'),
                "menus" => null
            ],
            //==============================================
//            ["title" => "资料管理",
            ["title" => "财务中心",
                "icon" => "icon-caiwuzhongxin",
                "url" => "#",
                "menus" => [
                    ["title" => "奖金记录",
                        "url" => route('web.bonus')],
                    ["title" => "消费记录",
                        "url" => route('web.shopping')],
                    ["title" => "会员转账",
                        "url" => route('web.transfer')],
                    ["title" => "转账记录",
                        "url" => route('web.transfer.log')],
                    ["title" => "申请提现",
                        "url" => route('web.withdraw')],
                    ["title" => "提现记录",
                        "url" => route('web.withdraw.log')],
                    ["title" => "币种转换",
                        "url" => route('web.switch.coin')],
                    ["title" => "转换记录",
                        "url" => route('web.switch.log')],
                    ["title" => "打款申请",
                        "url" => route('web.remit.apply')],
                    ["title" => "打款申请记录",
                        "url" => route('web.remit.logs')],
                ]],
            //==============================================
//            ["title" => "会员管理",
            ["title" => "会员管理",
                "icon" => "icon-huiyuanguanli",
                "url" => "#",
                "menus" => [
                    ["title" => "注册新会员",
                        "url" => route('web.reg.page'),
//                  "new"   => true
                    ],
                    ["title" => "会员激活",
                        "url" => route('web.users')],
                    ["title" => "已激活会员",
                        "url" => route('web.active-users.list')],
                    ["title" => "我的推荐会员",
                        "url" => route('web.p-users.list')],
                    ["title" => "申请报单中心",
                        "url" => route('web.bdzx')],
                    ["title" => "覆盖升级",
                        "url" => route('web.up-user'),
                    ],
                    ["title" => "会员节点关系图",
                        "url" => route('web.node.tree')],
                    ["title" => "会员推荐关系图",
                        "url" => route('web.node.zTree')],
                ]],
            //==============================================
//            ["title" => "财务中心",
            ["title" => "虚拟货币",
                "icon" => "icon-wxbbaobiao",
                "url" => "#",
                "menus" => [
                    ["title" => "交易大厅",
                        "url" => route('sys.stock')],
//                 ["title" => "我的持仓",
//                  "url"   => route('sys.stock')],
                ]],
            //==============================================
            ["title" => "购物商城",
                "icon" => "icon-shangcheng1",
                "url" => "#",
                "menus" => [
                    ["title" => "购物中心",
                        "url" => route('web.products')],
                    ["title" => "我的购物车",
                        "url" => route('web.carts')],

                    ["title" => "我的订单",
                        "url" => route('web.orders')],
                    ["title" => "我的收货地址",
                        "url" => route('web.address')]
                ]],
            //==============================================
            ["title" => "个人信息",
                "icon" => "icon-ziliaoguanli",
                "url" => "#",
                "menus" => [
                    ["title" => "我的信息修改",
                        "url" => route('web.information')],
                    ["title" => "修改密码",
                        "url" => route('web.pass')]
                ]],
            //==============================================
            ["title" => "关于我们",
                "icon" => "icon-icon071",
                "url" => "#",
                "menus" => [
                    ["title" => "新闻动态",
                        "url" => route('web.news', ['flag' => 0])],
                    ["title" => "系统公告",
                        "url" => route('web.news', ['flag' => 1])],
                    ["title" => "在线留言",
                        "url" => route('web.feedback')]
                ]],
            //==============================================
            ["title" => "安全退出",
                "icon" => "icon-fcstubiao24",
                "url" => route('sys.login.exit')
            ],
        );
        return $menu;
    }

    /**
     * 根据管理员权限生成可操作的菜单数组
     */
    public static function adminCanMenus(Admin $admin)
    {
        $allMenus = self::adminMenu();
        $result = array();
        foreach ($allMenus as $tab) {
            if ($admin->rolesCan($tab["permission"])) {
                $tabs = array();
                $tabs['title'] = $tab['title'];
                $tabs['icon'] = asset($tab['icon']);
                $tabs['url'] = $tab['url'];
                $tabs['groups'] = [];
                $groups = array();
                foreach ($tab['menus'] as $menus) {
                    $groupItem = array();
                    $groupItem['title'] = $menus['title'];
                    $groupItem['childs'] = array();
                    $childs = [];
                    foreach ($menus['menus'] as $item) {
                        if ($admin->rolesCan($item['permission'])) {
                            $childItem = [];
                            $childItem['title'] = $item['title'];
                            $childItem['url'] = $item['url'];
                            $childs[] = $childItem;
                        }
                    }
                    if (!empty($childs)) {
                        $groupItem['childs'] = $childs;
                        $groups[] = $groupItem;
                    }
                }
                if (!empty($groups)) {   //只有TAB下有菜单项才加入菜单
                    $tabs['groups'] = $groups;
                    $result[] = $tabs;
                }
            }
        }
        return $result;
    }
}